<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.array2string &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Input and output" href="../routines.io.html" >
    <link rel="next" title="numpy.array_repr" href="numpy.array_repr.html" >
    <link rel="prev" title="numpy.fromregex" href="numpy.fromregex.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.io.html" accesskey="U">Input and output</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.array_repr.html" title="numpy.array_repr"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.fromregex.html" title="numpy.fromregex"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.fromregex.html"
                        title="previous chapter">numpy.fromregex</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.array_repr.html"
                        title="next chapter">numpy.array_repr</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-array2string">
<h1>numpy.array2string<a class="headerlink" href="#numpy-array2string" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.array2string">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">array2string</code><span class="sig-paren">(</span><em class="sig-param">a</em>, <em class="sig-param">max_line_width=None</em>, <em class="sig-param">precision=None</em>, <em class="sig-param">suppress_small=None</em>, <em class="sig-param">separator=' '</em>, <em class="sig-param">prefix=''</em>, <em class="sig-param">style=&lt;no value&gt;</em>, <em class="sig-param">formatter=None</em>, <em class="sig-param">threshold=None</em>, <em class="sig-param">edgeitems=None</em>, <em class="sig-param">sign=None</em>, <em class="sig-param">floatmode=None</em>, <em class="sig-param">suffix=''</em>, <em class="sig-param">**kwarg</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/core/arrayprint.py#L532-L712"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.array2string" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representation of an array.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>a</strong><span class="classifier">array_like</span></dt><dd><p>Input array.</p>
</dd>
<dt><strong>max_line_width</strong><span class="classifier">int, optional</span></dt><dd><p>Inserts newlines if text is longer than <em class="xref py py-obj">max_line_width</em>.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['linewidth']</span></code>.</p>
</dd>
<dt><strong>precision</strong><span class="classifier">int or None, optional</span></dt><dd><p>Floating point precision.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['precision']</span></code>.</p>
</dd>
<dt><strong>suppress_small</strong><span class="classifier">bool, optional</span></dt><dd><p>Represent numbers “very close” to zero as zero; default is False.
Very close is defined by precision: if the precision is 8, e.g.,
numbers smaller (in absolute value) than 5e-9 are represented as
zero.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['suppress']</span></code>.</p>
</dd>
<dt><strong>separator</strong><span class="classifier">str, optional</span></dt><dd><p>Inserted between elements.</p>
</dd>
<dt><strong>prefix</strong><span class="classifier">str, optional</span></dt><dd></dd>
<dt><strong>suffix: str, optional</strong></dt><dd><p>The length of the prefix and suffix strings are used to respectively
align and wrap the output. An array is typically printed as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">prefix</span> <span class="o">+</span> <span class="n">array2string</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="n">suffix</span>
</pre></div>
</div>
<p>The output is left-padded by the length of the prefix string, and
wrapping is forced at the column <code class="docutils literal notranslate"><span class="pre">max_line_width</span> <span class="pre">-</span> <span class="pre">len(suffix)</span></code>.
It should be noted that the content of prefix and suffix strings are
not included in the output.</p>
</dd>
<dt><strong>style</strong><span class="classifier">_NoValue, optional</span></dt><dd><p>Has no effect, do not use.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 1.14.0.</span></p>
</div>
</dd>
<dt><strong>formatter</strong><span class="classifier">dict of callables, optional</span></dt><dd><p>If not None, the keys should indicate the type(s) that the respective
formatting function applies to.  Callables should return a string.
Types that are not specified (by their corresponding keys) are handled
by the default formatters.  Individual types for which a formatter
can be set are:</p>
<ul class="simple">
<li><p>‘bool’</p></li>
<li><p>‘int’</p></li>
<li><p>‘timedelta’ : a <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.timedelta64</span></code></p></li>
<li><p>‘datetime’ : a <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.datetime64</span></code></p></li>
<li><p>‘float’</p></li>
<li><p>‘longfloat’ : 128-bit floats</p></li>
<li><p>‘complexfloat’</p></li>
<li><p>‘longcomplexfloat’ : composed of two 128-bit floats</p></li>
<li><p>‘void’ : type <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.void</span></code></p></li>
<li><p>‘numpystr’ : types <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.string_</span></code> and <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.unicode_</span></code></p></li>
<li><p>‘str’ : all other strings</p></li>
</ul>
<p>Other keys that can be used to set a group of types at once are:</p>
<ul class="simple">
<li><p>‘all’ : sets all types</p></li>
<li><p>‘int_kind’ : sets ‘int’</p></li>
<li><p>‘float_kind’ : sets ‘float’ and ‘longfloat’</p></li>
<li><p>‘complex_kind’ : sets ‘complexfloat’ and ‘longcomplexfloat’</p></li>
<li><p>‘str_kind’ : sets ‘str’ and ‘numpystr’</p></li>
</ul>
</dd>
<dt><strong>threshold</strong><span class="classifier">int, optional</span></dt><dd><p>Total number of array elements which trigger summarization
rather than full repr.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['threshold']</span></code>.</p>
</dd>
<dt><strong>edgeitems</strong><span class="classifier">int, optional</span></dt><dd><p>Number of array items in summary at beginning and end of
each dimension.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['edgeitems']</span></code>.</p>
</dd>
<dt><strong>sign</strong><span class="classifier">string, either ‘-‘, ‘+’, or ‘ ‘, optional</span></dt><dd><p>Controls printing of the sign of floating-point types. If ‘+’, always
print the sign of positive values. If ‘ ‘, always prints a space
(whitespace character) in the sign position of positive values.  If
‘-‘, omit the sign character of positive values.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['sign']</span></code>.</p>
</dd>
<dt><strong>floatmode</strong><span class="classifier">str, optional</span></dt><dd><p>Controls the interpretation of the <em class="xref py py-obj">precision</em> option for
floating-point types.
Defaults to <code class="docutils literal notranslate"><span class="pre">numpy.get_printoptions()['floatmode']</span></code>.
Can take the following values:</p>
<ul class="simple">
<li><p>‘fixed’: Always print exactly <em class="xref py py-obj">precision</em> fractional digits,
even if this would print more or fewer digits than
necessary to specify the value uniquely.</p></li>
<li><p>‘unique’: Print the minimum number of fractional digits necessary
to represent each value uniquely. Different elements may
have a different number of digits.  The value of the
<em class="xref py py-obj">precision</em> option is ignored.</p></li>
<li><p>‘maxprec’: Print at most <em class="xref py py-obj">precision</em> fractional digits, but if
an element can be uniquely represented with fewer digits
only print it with that many.</p></li>
<li><p>‘maxprec_equal’: Print at most <em class="xref py py-obj">precision</em> fractional digits,
but if every element in the array can be uniquely
represented with an equal number of fewer digits, use that
many digits for all elements.</p></li>
</ul>
</dd>
<dt><strong>legacy</strong><span class="classifier">string or <em class="xref py py-obj">False</em>, optional</span></dt><dd><p>If set to the string <em class="xref py py-obj">‘1.13’</em> enables 1.13 legacy printing mode. This
approximates numpy 1.13 print output by including a space in the sign
position of floats and different behavior for 0d arrays. If set to
<em class="xref py py-obj">False</em>, disables legacy mode. Unrecognized strings will be ignored
with a warning for forward compatibility.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.14.0.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>array_str</strong><span class="classifier">str</span></dt><dd><p>String representation of the array.</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>TypeError</strong></dt><dd><p>if a callable in <a class="reference external" href="https://docs.python.org/dev/library/formatter.html#module-formatter" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">formatter</span></code></a> does not return a string.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="numpy.array_str.html#numpy.array_str" title="numpy.array_str"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_str</span></code></a>, <a class="reference internal" href="numpy.array_repr.html#numpy.array_repr" title="numpy.array_repr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_repr</span></code></a>, <a class="reference internal" href="numpy.set_printoptions.html#numpy.set_printoptions" title="numpy.set_printoptions"><code class="xref py py-obj docutils literal notranslate"><span class="pre">set_printoptions</span></code></a>, <a class="reference internal" href="numpy.get_printoptions.html#numpy.get_printoptions" title="numpy.get_printoptions"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_printoptions</span></code></a></p>
</div>
<p class="rubric">Notes</p>
<p>If a formatter is specified for a certain type, the <em class="xref py py-obj">precision</em> keyword is
ignored for that type.</p>
<p>This is a very flexible function; <a class="reference internal" href="numpy.array_repr.html#numpy.array_repr" title="numpy.array_repr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_repr</span></code></a> and <a class="reference internal" href="numpy.array_str.html#numpy.array_str" title="numpy.array_str"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_str</span></code></a> are using
<a class="reference internal" href="#numpy.array2string" title="numpy.array2string"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array2string</span></code></a> internally so keywords with the same name should work
identically in all three functions.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1e-16</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array2string</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">precision</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">separator</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">,</span>
<span class="gp">... </span>                      <span class="n">suppress_small</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">&#39;[0.,1.,2.,3.]&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>  <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">3.</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array2string</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">formatter</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;float_kind&#39;</span><span class="p">:</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%.2f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">x</span><span class="p">})</span>
<span class="go">&#39;[0.00 1.00 2.00]&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>  <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array2string</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">formatter</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;int&#39;</span><span class="p">:</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">hex</span><span class="p">(</span><span class="n">x</span><span class="p">)})</span>
<span class="go">&#39;[0x0 0x1 0x2]&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>